﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CrashMeManaged
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class CrashMeWindow : Window
    {
        public Exception ExceptionToTrow { get; set; }

        public Action<Exception> ThrowException { get; set; }

        private CrashMeWindow()
        {
            InitializeComponent();
        }

        public CrashMeWindow(Action<Exception> throwException = null)
        {
            ThrowException = throwException;

            InitializeComponent();
        }

        private void ValidateResult()
        {
            this.Hide();

            if (ThrowException != null)
            {
                ThrowException(ExceptionToTrow);
            }

            //if (System.Windows.Interop.ComponentDispatcher.IsThreadModal)
            //{
            //    this.DialogResult = true;
            //}
        }

        private void NullReferenceException_Click(object sender, RoutedEventArgs e)
        {
            ExceptionToTrow = new NullReferenceException();

            ValidateResult();
        }

        private void NotImplementedException_Click(object sender, RoutedEventArgs e)
        {
            ExceptionToTrow = new NotImplementedException();

            ValidateResult();
        }

        private void AccessViolationException_Click(object sender, RoutedEventArgs e)
        {
            ExceptionToTrow = new AccessViolationException();

            ValidateResult();
        }

        private void ArgumentNullException_Click(object sender, RoutedEventArgs e)
        {
            ExceptionToTrow = new ArgumentNullException();

            ValidateResult();
        }

        private void ArgumentOutOfRangeException_Click(object sender, RoutedEventArgs e)
        {
            ExceptionToTrow = new ArgumentOutOfRangeException();

            ValidateResult();
        }

        private void DivideByZeroException_Click(object sender, RoutedEventArgs e)
        {
            ExceptionToTrow = new DivideByZeroException();

            ValidateResult();
        }

        private void IndexOutOfRangeException_Click(object sender, RoutedEventArgs e)
        {
            ExceptionToTrow = new IndexOutOfRangeException();

            ValidateResult();
        }

        private void InvalidOperationException_Click(object sender, RoutedEventArgs e)
        {
            ExceptionToTrow = new InvalidOperationException();

            ValidateResult();
        }

        private void OutOfMemoryException_Click(object sender, RoutedEventArgs e)
        {
            ExceptionToTrow = new InvalidOperationException();

            ValidateResult();
        }
    }
}
